﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using Tesla.Xna.Util;
using XFG = Microsoft.Xna.Framework.Graphics;

namespace Tesla.Xna.Graphics.Implementation {
    /// <summary>
    /// Concrete XNA 4.0 implementation for <see cref="DepthStencilState"/>.
    /// </summary>
    public sealed class XNADepthStencilStateImplementation : DepthStencilStateImplementation {
        private XFG.DepthStencilState _depthStencilState;

        /// <summary>
        /// Gets the actual XNA DepthStencilState object.
        /// </summary>
        internal XFG.DepthStencilState XNADepthStencilState {
            get {
                return _depthStencilState;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="XNADepthStencilStateImplementation"/>.
        /// </summary>
        internal XNADepthStencilStateImplementation() : base() { }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="XNADepthStencilStateImplementation"/> is reclaimed by garbage collection.
        /// </summary>
        ~XNADepthStencilStateImplementation() {
            Dispose(false);
        }

        /// <summary>
        /// Binds the implementation. This is called the first time an unbound state is set to the device or manually by the user in order to
        /// create the underlying state ahead of time (best practice). Once called the state properties are read-only.
        /// </summary>
        public override void BindDepthStencilState() {
            if(!base.IsBound) {
                _depthStencilState = new XFG.DepthStencilState();
                _depthStencilState.DepthBufferEnable = base.DepthEnable;
                _depthStencilState.DepthBufferWriteEnable = base.DepthWriteEnable;
                _depthStencilState.StencilEnable = base.StencilEnable;
                _depthStencilState.TwoSidedStencilMode = base.TwoSidedStencilEnable;
                _depthStencilState.StencilMask = base.StencilReadMask;
                _depthStencilState.StencilWriteMask = base.StencilWriteMask;
                _depthStencilState.ReferenceStencil = base.ReferenceStencil;
                _depthStencilState.CounterClockwiseStencilPass = XNAHelper.ToXNAStencilOperation(base.CounterClockwiseStencilPass);
                _depthStencilState.CounterClockwiseStencilDepthBufferFail = XNAHelper.ToXNAStencilOperation(base.CounterClockwiseStencilDepthFail);
                _depthStencilState.CounterClockwiseStencilFail = XNAHelper.ToXNAStencilOperation(base.CounterClockwiseStencilFail);
                _depthStencilState.CounterClockwiseStencilFunction = XNAHelper.ToXNACompareFunction(base.CounterClockwiseStencilFunction);
                _depthStencilState.DepthBufferFunction = XNAHelper.ToXNACompareFunction(base.DepthFunction);
                _depthStencilState.StencilFunction = XNAHelper.ToXNACompareFunction(base.StencilFunction);
                _depthStencilState.StencilDepthBufferFail = XNAHelper.ToXNAStencilOperation(base.StencilDepthFail);
                _depthStencilState.StencilFail = XNAHelper.ToXNAStencilOperation(base.StencilFail);
                _depthStencilState.StencilPass = XNAHelper.ToXNAStencilOperation(base.StencilPass);
                base.IsBound = true;
            }
        }
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                //Dispose of managed resources
                if(disposing) {
                    if(_depthStencilState != null) {
                        _depthStencilState.Dispose();
                    }
                }
            }
            base.Dispose(disposing);
        }
    }
}
